package com.dlry.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dlry.db.MeetingRoomBookingRepository;
import com.dlry.entity.DlMeetingRoomBooking;

@Service
@Transactional
public class MeetingRoomBookingServiceImpl implements MeetingRoomBookingService {
	@Autowired
	private MeetingRoomBookingRepository bookingRepository;

	public Boolean save(DlMeetingRoomBooking booking) {
		DlMeetingRoomBooking booking2 = bookingRepository.validateBooking(booking.getBookingTime(),
				booking.getEndTime(), booking.getRoom());
		if (booking2 != null) {
			return false;
		} else {
			bookingRepository.save(booking);
			return true;
		}
	}

	public Boolean update(DlMeetingRoomBooking booking) {
		DlMeetingRoomBooking booking2 = bookingRepository.validateBookingUpdate(booking.getBookingTime(),
				booking.getEndTime(), booking.getRoom(), booking.getId());
		if (booking2 != null) {
			return false;
		} else {
			bookingRepository.save(booking);
			return true;
		}
	}

	public void deleteByBookingId(Long bId) {
		bookingRepository.delete(bId);
	}

	public DlMeetingRoomBooking findById(Long bId) {
		return bookingRepository.findOne(bId);
	}

	public Page<DlMeetingRoomBooking> findAll(Integer page, Integer row) {
		Sort sort = new Sort(Direction.DESC, new String[] { "room", "bookingTime" });
		Pageable pageable = new PageRequest(page, row, sort);
		Page<DlMeetingRoomBooking> bookings = bookingRepository.findAll(pageable);
		return bookings;
	}

}
